package com.airnews.ticket.sql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class OrderDB {
	private static final String ORDER_TABLE_NAME = "orders";

	public static void insert(Context context, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		long id = mOpenHelper.getReadableDatabase().insert(ORDER_TABLE_NAME,
				"id", values);
		mOpenHelper.getReadableDatabase().close();
	}

	public static void delete(Context context, int id) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		mOpenHelper.getWritableDatabase().delete(ORDER_TABLE_NAME, "id=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}

	public static List<Map<String, String>> select(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		String[] columns = new String[] { "id", "orderId", "lname", "flightNo",
				"payed", "ordered", "bak", "fromcity", "tocity", "fromdate",
				"jclass", "fromtime", "totime", "aircraft", "totalfee", "tel" };
		Cursor c = mOpenHelper.getReadableDatabase().query(ORDER_TABLE_NAME,
				columns, null, null, null, null, null);
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", String.valueOf(c.getInt(0)));
			map.put("orderId", c.getString(1));
			map.put("lname", c.getString(2));
			map.put("flightNo", c.getString(3));
			map.put("payed", c.getString(4));
			map.put("ordered", c.getString(5));
			map.put("bak", c.getString(6));
			map.put("fromcity", c.getString(7));
			map.put("tocity", c.getString(8));
			map.put("fromdate", c.getString(9));
			map.put("jclass", c.getString(10));
			map.put("fromtime", c.getString(11));
			map.put("totime", c.getString(12));
			map.put("aircraft", c.getString(13));
			map.put("totalfee", c.getString(14));
			map.put("tel", c.getString(15));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data;
	}

	public static Map<String, String> select(Context context, int id) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		String[] columns = new String[] { "id", "orderId", "lname", "flightNo",
				"payed", "ordered", "bak", "fromcity", "tocity", "fromdate",
				"jclass", "fromtime", "totime", "aircraft", "totalfee", "tel" };
		Cursor c = mOpenHelper.getReadableDatabase().query(ORDER_TABLE_NAME,
				columns, "id=?", new String[] { id + "" }, null, null, null);
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", String.valueOf(c.getInt(0)));
			map.put("orderId", c.getString(1));
			map.put("lname", c.getString(2));
			map.put("flightNo", c.getString(3));
			map.put("payed", c.getString(4));
			map.put("ordered", c.getString(5));
			map.put("bak", c.getString(6));
			map.put("fromcity", c.getString(7));
			map.put("tocity", c.getString(8));
			map.put("fromdate", c.getString(9));
			map.put("jclass", c.getString(10));
			map.put("fromtime", c.getString(11));
			map.put("totime", c.getString(12));
			map.put("aircraft", c.getString(13));
			map.put("totalfee", c.getString(14));
			map.put("tel", c.getString(15));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data.get(0);
	}

}
